package com.ruoyi.web.controller.business.goods.factory;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.business.goods.domain.JbCommodityOfflinePrice;
import com.ruoyi.business.goods.service.JbCommodityOfflinePriceService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.core.JoController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 业务 线下商品价格 工厂
 *
 * @author Jyl
 */
@Component
public class JbCommodityOfflinePriceFactory extends JoController
{

    @Autowired
    private JbCommodityOfflinePriceService jbCommodityOfflinePriceService;

    /**
     * @param id     商品主键
     * @param deptId 部门id
     * @return 商品线下价格
     */
    public List<JbCommodityOfflinePrice> list(Long id, String deptId)
    {
        LambdaQueryWrapper<JbCommodityOfflinePrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JbCommodityOfflinePrice::getCommodityId, id);
        List<JbCommodityOfflinePrice> list = jbCommodityOfflinePriceService.list(queryWrapper);

        //如果需要部门过滤
        if (StrUtil.isNotEmpty(deptId))
        {
            //默认给第一个价格
            List<JbCommodityOfflinePrice> deptPrice = new ArrayList<>();
            deptPrice.add(list.get(0));
            for (JbCommodityOfflinePrice jbCommodityOfflinePrice : list)
            {
                //遍历比较部门id，返回匹配价格
                String[] depts = jbCommodityOfflinePrice.getDepartmentIds().split(",");
                for (String dept : depts)
                {
                    if (deptId.equals(dept))
                    {
                        deptPrice = new ArrayList<>();
                        deptPrice.add(jbCommodityOfflinePrice);
                        return deptPrice;
                    }
                }
            }
            return deptPrice;
        } else
        {
            return list;
        }
    }

    /**
     * 编辑商品线下价格
     */
    public boolean edits(List<JbCommodityOfflinePrice> list, Long id) throws InvocationTargetException,
            NoSuchMethodException, IllegalAccessException
    {
        //赋值父主键
        for (JbCommodityOfflinePrice jbCommodityOfflinePrice : list)
        {
            jbCommodityOfflinePrice.setCommodityId(id);
        }

        //删除历史数据
        List<Long> ids = ids(list, "id");
        if (StringUtils.isNotEmpty(ids))
        {
            jbCommodityOfflinePriceService.remove(new LambdaUpdateWrapper<JbCommodityOfflinePrice>()
                    .notIn(JbCommodityOfflinePrice::getId, ids)
                    .eq(JbCommodityOfflinePrice::getCommodityId, id)
            );
        }

        //添加更新
        return jbCommodityOfflinePriceService.saveOrUpdateBatch(list);
    }


    /**
     * 新增商品线下价格
     */
    @Transactional
    public Boolean adds(List<JbCommodityOfflinePrice> list, Long pid)
    {
        for (JbCommodityOfflinePrice jbCommodityOfflinePrice : list)
        {
            jbCommodityOfflinePrice.setCommodityId(pid);
        }
        return jbCommodityOfflinePriceService.saveBatch(list);
    }

    /**
     * 查询商品线下价格
     */
    public List<JbCommodityOfflinePrice> list(Long pid)
    {
        LambdaQueryWrapper<JbCommodityOfflinePrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(JbCommodityOfflinePrice::getCommodityId, pid);
        return jbCommodityOfflinePriceService.list(queryWrapper);
    }

}
